﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.Configuration;
using NeosSDI.Core.Manager;

namespace NeosSDI.DAL.ADO
{
    public class DatabaseQuery
    {
        public List<SqlQuery> Queries { get; set; }
        public List<StoredProcedure> StoredProcedure { get; set; }


        public static DatabaseQuery Load()
        {
            DatabaseQuery p = null;
            XmlSerializer xs = new XmlSerializer(typeof(DatabaseQuery));

            string filePath = ConfigurationManager.AppSettings[WebConfig.Configuration.AppSettings.DatabaseQuery.ToDescription()];
            using (StreamReader rd = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/DatabaseQuery.xml")))
            {
                p = xs.Deserialize(rd) as DatabaseQuery;
            }
            return p;
        }

        internal string GetQuery(string queryName)
        {
            return Queries.Where(p => p.Name == queryName).Select(p => p.Query).SingleOrDefault();
        }

        internal string GetStoredProcedured(string storedProceduredName)
        {
            return StoredProcedure.Where(p => p.Name == storedProceduredName).Select(p => p.StoredProcedureName).SingleOrDefault();
        }
    }

    public class SqlQuery
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string Query { get; set; }
    }

    public class StoredProcedure
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public string StoredProcedureName { get; set; }
    }
}